package com.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.DBUtils.DBUtil;

/**
 * Servlet implementation class ExchangeServlet
 */
@WebServlet("/ExchangeServlet")
public class ExchangeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ExchangeServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String i=request.getParameter("id");
		int id=Integer.parseInt(i);
		String txt=request.getParameter("na");
		HttpSession session=request.getSession();
		
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		String date=sdf.format(new Date());
		
		try {
			Connection con=DBUtil.getconn();
			StringBuffer sql=new StringBuffer("insert into(exchangeTxt,exchangeName,exchangeDate,FK_r) values('");
			sql.append(txt+"','");
			sql.append(session.getAttribute("name")+"','");
			sql.append(date+"',");
			sql.append(id+")");
			System.out.println(sql.toString());
			Statement stm = con.createStatement();
			stm.executeUpdate(sql.toString());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
